package cn.dakaqi.pingan.util;

import cn.dakaqi.pingan.entities.ActivateResult;
import cn.dakaqi.pingan.entities.PingAnUser;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.InputSource;

import java.io.StringReader;

/**
 * @author chunyang on 2016/2/17.
 */
public class ParseXML {
    /**
     * 开户返回的XML
     *
     * @param strXML 开户返回的XML
     * @return 开户信息
     */
    public static PingAnUser openResult(String strXML) {
//        【样例报文】：
//        <?xml version="1.0" encoding="UTF-8"?>
//        <ROOT>
//        <state>99</state>
//        <cardNo></cardNo>
//        <isNew></isNew>
//        <returnUrl></returnUrl>
//        <errorCode></errorCode>
//        <errorMessage></errorMessage>
//        </ROOT>

        PingAnUser pingAnUser = null;
        SAXReader reader = new SAXReader();
        StringReader sr = new StringReader(strXML);
        InputSource is = new InputSource(sr);
        try {
            Document document = reader.read(is);
            Element root = document.getRootElement();
            Element stateElement = root.element("state");
            Element cardNoElement = root.element("cardNo");
            Element isNewElement = root.element("isNew");
            Element returnUrlElement = root.element("returnUrl");
            Element errorCodeElement = root.element("errorCode");
            Element errorMessageElement = root.element("errorMessage");
            pingAnUser = new PingAnUser(errorMessageElement.getText(), errorCodeElement.getText(), stateElement.getText(), isNewElement.getText(), returnUrlElement.getText(), cardNoElement.getText());
//
//            //get element
//            List<Element> phoneList = root.elements("TelePhone");
//            List<Element> typeList = phoneList.get(0).elements("type");
//            for (int i = 0; i < typeList.size(); i++)
//            {
//                Element element = typeList.get(i);
//                String phoneName = element.attributeValue("name");
//                logger.info("phonename = " + phoneName);
//                //get all element
//                List<Element> childList = element.elements();
//                for (int j = 0; j < childList.size(); j++)
//                {
//                    Element e = childList.get(j);
//                    logger.info(e.getName() + "=" + e.getText());
//                }
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pingAnUser;
    }

    /**
     * 激活返回的XML
     *
     * @param strXML 激活返回的xml
     * @return 激活信息
     */
    public static ActivateResult activateResult(String strXML) {
//        【样例报文】：
//        <?xml version="1.0" encoding="UTF-8"?>
//        <ROOT>
//        <state></state>
//        <returnUrl></returnUrl>
//        <errorCode></errorCode>
//        <errorMessage></errorMessage>
//        </ROOT>

        strXML = strXML.replace("&", "&amp;");
        ActivateResult result;
        SAXReader reader = new SAXReader();
        StringReader sr = new StringReader(strXML);
        InputSource is = new InputSource(sr);
        try {
            Document document = reader.read(is);
            Element root = document.getRootElement();
            Element stateElement = root.element("state");
            Element returnUrlElement = root.element("returnUrl");
            Element errorCodeElement = root.element("errorCode");
            Element errorMessageElement = root.element("errorMessage");
            Element cardStateElement = root.element("cardState");
            result = new ActivateResult(stateElement.getText(), returnUrlElement.getText(), errorCodeElement.getText(), errorMessageElement.getText(), cardStateElement.getText());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return result;
    }

    public static String thirdVoucher(String strXML)
    {
        String thirdVoucherSN = null;
        strXML = strXML.replace("&", "&amp;");
        SAXReader reader = new SAXReader();
        StringReader sr = new StringReader(strXML);
        InputSource is = new InputSource(sr);
        try {
            Document document = reader.read(is);
            Element root = document.getRootElement();
            Element ThirdVoucherElement = root.element("ThirdVoucher");
            thirdVoucherSN = ThirdVoucherElement.getText();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return thirdVoucherSN;
        }
        return thirdVoucherSN;
    }
}
